package com.swas.controller;

import com.github.pagehelper.PageInfo;
import com.swas.entity.PatrolTaskDTO;
import com.swas.entity.PatrolTask;
import com.swas.entity.User;
import com.swas.service.PatrolTaskService;
import com.swas.util.ResultVO;
import com.swas.vo.PatrolTaskVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/patrolTask")
public class PatrolTaskController {

    @Autowired
    private PatrolTaskService patrolTaskService;

    @PostMapping
    public ResultVO list(@RequestBody PatrolTaskDTO patrolTaskDTO, HttpSession session) {
        User userInfo = (User) session.getAttribute("userInfo");
        List<PatrolTask> list = patrolTaskService.list(patrolTaskDTO,userInfo);
        PageInfo<PatrolTask> pageInfo = new PageInfo<>(list);
        return ResultVO.success(pageInfo);
    }

    @PutMapping
    public ResultVO update(@RequestBody PatrolTaskDTO patrolTaskDTO) {
        patrolTaskService.update(patrolTaskDTO);
        return ResultVO.success();
    }

    @PostMapping("/add/{planId}")
    public ResultVO add(@PathVariable Integer planId, @RequestBody List<PatrolTaskDTO> patrolTaskDTOList, HttpSession session) {
        User userInfo = (User) session.getAttribute("userInfo");
        patrolTaskService.taskFP(planId, patrolTaskDTOList,userInfo);
        return ResultVO.success();
    }

    @GetMapping("/planId/{planId}")
    public ResultVO getByPlanId(@PathVariable Integer planId) {
        List<PatrolTaskVO> list = patrolTaskService.getByPlanId(planId);
        return ResultVO.success(list);
    }
}
